<?php

namespace App\Dao;

use App\Exceptions\SysExceptions;
use App\Model\TaskLogModel;
use App\Model\TaskModel;
use Illuminate\Support\Facades\DB;

class TaskDao extends BaseDao
{

    public static function get_task_builder($params = []): \Illuminate\Database\Eloquent\Builder
    {
        $query = TaskModel::query();
        $query->when(checkIsHasField($params,'task_id'),function ($query)use ($params){
            $query->where('task_id',$params['task_id']);
        });

        return $query;
    }





    public static function get_task_log_builder($params = []): \Illuminate\Database\Eloquent\Builder
    {
        $query = TaskLogModel::query();

        $query->when(checkIsHasField($params,'user_id'),function ($query)use ($params){
            $query->where('user_id',$params['user_id']);
        });

        $query->when(checkIsHasField($params,'task_id'),function ($query)use ($params){
            $query->where('task_id',$params['task_id']);
        });


        return $query;
    }

    public static function get_task_page(array $all): \Illuminate\Contracts\Pagination\LengthAwarePaginator
    {
        return self::get_task_builder($all)->paginate();
    }

    public static function add(array $params)
    {
        if(self::get_task_builder()->create($params) === false){
            self::error('添加失败');
        }
    }

    public static function update($params = []){
        if(self::get_task_builder(['task_id'=>self::deleteParamId($params,'task_id')])->update(self::filterFields($params,new TaskModel())) === false){
            self::error('修改失败');
        }
    }

    public static function get_task_one(array $all)
    {
        return self::get_task_builder($all)->first();
    }

    public static function get_tasks()
    {
        return self::get_task_builder()->whereNotIn('task_id',[4])->get();
    }


    public static function get_task_log($params = []){
        return self::get_task_log_builder([
            'user_id'=>$params['user_id']
        ])->with('taskInfo')->get();
    }


    /**
     * @throws SysExceptions
     */
    public static function add_task_log($params = []){
        if(self::get_task_log_builder([
            'user_id'=>$params['user_id'],
            'task_id'=>$params['task_id']
        ])->exists()){
            self::error('已经参加了');
        }
        DB::beginTransaction();
        self::emit_df($params['task_id'],$params['user_id']);
        DB::commit();
    }


    //获取释放的数值

    /**
     * @throws SysExceptions
     */
    public static function emit_df($task_id = 0, $user_id= 0){
        //获取任务
        $task_info = TaskDao::get_task_builder([
            'task_id'=>$task_id
        ])->first();
        $task_num = $task_info['task_num'];
        //获取我下面的组员
        $count = UsersDao::get_user_builder([
            'point_uid'=>$user_id
        ])->count();
        $task_num = $task_num + (2*$count);
        $userInfo = UsersDao::get_user_builder([
            'user_id'=>$user_id
        ])->first();
        if($userInfo['d_price'] < $task_num){
            $task_num = $userInfo['d_price'];
        }
        $res = UsersDao::get_user_builder([
            'user_id'=>$user_id
        ])->decrement('d_price',$task_num);
        $res !== false && $res = UsersDao::get_user_builder([
            'user_id'=>$user_id
        ])->increment('k_price',$task_num);
        $res !== false && $res = self::get_task_log_builder()->create([
            'task_id'=>$task_id,
            'user_id'=>$user_id,
            'status'=>1,
            'num'=>$task_num
        ]);
        //增加流水
        $res !== false && $res = BillDao::get_bill_builder()->create([
            'user_id'=>$user_id,
            'num'=>$task_num,
            'pm'=>0,
            'type'=>2,
            'title'=>'用户释放DF值'
        ]);

        $res !== false && $res = BillDao::get_bill_builder()->create([
            'user_id'=>$user_id,
            'num'=>$task_num,
            'pm'=>1,
            'type'=>1,
            'title'=>'用户释放DF值到佣金'
        ]);
        if($res === false){
            DB::rollBack();
            self::error('任务执行失败');
        }
        return $res;
    }
}
